package base.serialized;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class ExternalAlien {
	
	public static void main(String[] args) throws Exception{
		fileFreeze();
		fileUnfreeze();
		
//		byteUnfreeze(byteFreeze());
	}
	
	
	
	public static void fileFreeze() throws Exception {

		AlienExten alien = initAlien();

		ObjectOutputStream stream = new ObjectOutputStream(
				new FileOutputStream("alien.out"));

		stream.writeObject(alien);
	}

	public static ByteArrayOutputStream byteFreeze() throws Exception {

		AlienExten alien = initAlien();

		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		ObjectOutputStream stream = new ObjectOutputStream(bos);

		stream.writeObject(alien);

		return bos;
	}

	public static void fileUnfreeze() throws Exception {
		ObjectInputStream stream = new ObjectInputStream(new FileInputStream(
				"alien.out"));

		AlienExten alien = (AlienExten) stream.readObject();

		System.out.println(alien);
	}

	public static void byteUnfreeze(ByteArrayOutputStream bos) throws Exception{
		
		ObjectInputStream stream=new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
		
		AlienExten alien=(AlienExten) stream.readObject();
		
		System.out.println(alien);	
	}

	private static AlienExten initAlien() {
		AlienExten alien = new AlienExten();
		alien.setName("alienExten");
		alien.setProduct("wap");
		alien.setVersion("2.0.0");
		alien.setPwd("password");
		return alien;
	}
	
}
class AlienExten implements Externalizable{

	private String name;
	private String version;
	private String product;
	private String pwd;
	public AlienExten(){
		System.out.println("default constructor");
	}
	public AlienExten(String name,String product,String version){
		System.out.println("setting constructor");
		this.name=name;
		this.product=product;
		this.version=version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("object.writeExternal");
		out.writeObject(name);
		out.writeObject(product);
		out.writeObject(version);
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		System.out.println("object.readExternal");
		name=(String) in.readObject();
		product=(String) in.readObject();
		version=(String) in.readObject();
	}
	
	public String toString() {
		return "name:" + name + "\nversion:" + version + "\nproduct:" + product+"\npwd:"+pwd;
	}
}
